package com.baichang.taoshunda.service.impl;

import com.baichang.framework.utils.KidUtils;
import com.baichang.taoshunda.dao.CouponDao;
import com.baichang.taoshunda.dao.CouponDetailsDao;
import com.baichang.taoshunda.entity.Coupon;
import com.baichang.taoshunda.entity.CouponDetails;
import com.baichang.taoshunda.service.CouponService;
import com.baichang.taoshunda.utils.ConvertTypeUtils;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 17/09/08.
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponDao couponDao;

    @Autowired
    private CouponDetailsDao couponDetailsDao;

    @Override
    public List<Map<String, Object>> getCouponByBussId(int busId) {
        return couponDao.getCouponByBussId(busId);
    }

    /**
     * 根据状态查询商家的活动
     *
     * @param bussId 商家编号
     * @param state  状态
     *               0：待生效
     *               1：已生效
     *               2：已结束
     * @return
     */
    @Override
    public List<Map<String, Object>> findCouponByState(Integer bussId, Integer state) {
        return couponDao.findCouponByState(bussId, state);
    }

    /**
     * 添加优惠券
     *
     * @param bussId    商家编号
     * @param name      活动名称
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param days      有效期
     * @param jsonData  最低金额
     * @return
     */
    @Override
    public boolean addCoupon(Integer bussId, String name, String beginTime, String endTime, Integer days, String jsonData) {
        JSONArray jsonArray = JSONArray.fromObject(jsonData);
        String kid = KidUtils.getKKid();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < jsonArray.size(); i++) {
            Map<String, Object> map = (Map<String, Object>) jsonArray.get(i);
            Double minMoney = ConvertTypeUtils.obj2Double(map.get("minMoney"));
            Double subMoney = ConvertTypeUtils.obj2Double(map.get("subMoney"));
            Coupon coupon = new Coupon();
            coupon.setBussId(bussId);
            coupon.setCreated(new Date());
            coupon.setName("满" + minMoney + "减" + subMoney);
            coupon.setReceiveEndTime(ConvertTypeUtils.obj2DateFormat(endTime));
            coupon.setStartTime(ConvertTypeUtils.obj2DateFormat(beginTime));
            coupon.setValidityDays(days);
            coupon.setMinMoney(BigDecimal.valueOf(minMoney));
            coupon.setSubMoney(BigDecimal.valueOf(subMoney));
            coupon.setCouponKid(kid);
            couponDao.save(coupon);
            CouponDetails couponDetails = new CouponDetails();
            couponDetails.setKid(kid);
            couponDetails.setMinMoney(BigDecimal.valueOf(minMoney));
            couponDetails.setSubMoney(BigDecimal.valueOf(subMoney));
            couponDetailsDao.save(couponDetails);
        }
        return true;
    }

    /**
     * 根据商家编号获取商家满减的kid
     *
     * @param bussId 商家编号
     * @return
     */
    @Override
    public List<Map<String, Object>> getCouponKidByBussId(Integer bussId) {
        return couponDao.getCouponKidByBussId(bussId);
    }

    /**
     * 根据kid获取满减
     *
     * @param couponKid kid
     * @return
     */
    @Override
    public List<Coupon> findCouponByKid(String couponKid) {
        return couponDao.findCouponByKid(couponKid);
    }
}
